package com.congee02.dbservlet.servlet;

import com.congee02.dbservlet.framework.ioc.annotation.Autowired;
import com.congee02.dbservlet.framework.ioc.annotation.Component;
import com.congee02.dbservlet.framework.ioc.annotation.WebServlet;
import com.congee02.dbservlet.service.update.UpdateService;
import com.congee02.dbservlet.service.update.UpdateServiceImpl;
import com.congee02.dbservlet.utils.ResponseUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@WebServlet(mappings = "/update")
public class UpdatePasswordServlet extends HttpServlet {

    @Autowired
    private UpdateServiceImpl updateService;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        String oldPwd = req.getParameter("oldPwd");
        String newPwd = req.getParameter("newPwd");

        ResponseUtils.writeJsonAndClose(resp, updateService.updatePassword(username, oldPwd, newPwd));
    }
}
